﻿/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.display 
{
	import stencil.core.engine.Box;
	import stencil.core.engine.IGameObject;
	import stencil.core.engine.ISceneObject;
	import stencil.core.engine.Position;
	
	/**
	 * Default implementation of IDepthSort interface.
	 * 
	 * z-index of object Foo is calculated by next formula:
	 * 
	 * z-index(Foo) = Foo.x + Foo.y + Foo.width/2 + Foo.height/2
	 * 
	 * Sorting process uses method <code>sort</a> of Array class using the next compare function.
	 * Let's assume that A, B are arguments of this compare function then:
	 * 
	 * a) if A is not a tile and B is then A should be placed in front of B in display list 
	 * b) if A is not a background object and  B is then A should be placed in front of B in display list 
	 * c) if z-index(A) is greater then z-index(B) then A should be placed in front of B in display list 
	 * d) if z-index(A) is less then z-index(B) then B should be placed in front of B in display list 
	 * 
	 * if z-index(A) equals to then z-index(B):
	 * e1) A.priority > B.priority then A should be placed in front of B in display list
	 * e2) A.priority < B.priority then B should be placed in front of A in display list
	 * 
	 * Both z-index and priority of the two objects equal:
	 * f) if A.y > B.y or A.x > B.x  then A should be placed in front of B in display list 
	 * 
	 * NOTICE: objects with not equal width and height can be displayed in incorrect order.
	 * 
	 * @author Max Zhuravkov
	 */
	public class DefaultDepthSorting implements IDepthSorting
	{
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		public function DefaultDepthSorting() {
			
		}
		
		//------------------------------------------------------------------------------------------------
		// Public methods
		//------------------------------------------------------------------------------------------------
		
		public function sort(objects:Array):void{
			objects.sort(this.compare);
		}
		
		public function calculateIndex(object:IDisplayObject):void {
			var state:DisplayState = object.displayState;
			var sceneObject:ISceneObject = object.owner;
			var position:Position = state.position;
			var dimensions:Box = sceneObject.dimensions;
			var px:Number = position.x;
			var py:Number = position.y;
			
			if (dimensions.dx > 1 && dimensions.dy > 1) {
				px = px + dimensions.dx/2;
				py = py + dimensions.dy/2;
			}
			
			state.zIndex = px + py;
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected methods
		//------------------------------------------------------------------------------------------------
		
		protected function compare(a:IDisplayObject, b:IDisplayObject):Number {
			var ao:ISceneObject = a.owner;
			var bo:ISceneObject = b.owner;
			
			if (!ao.flagTile && bo.flagTile || !ao.flagBackground && ao.flagBackground) {
				return 1;
			}
			
			var ap:Position = ao.position;
			var bp:Position = bo.position;
			var sa:DisplayState = a.displayState;
			var sb:DisplayState = b.displayState;
			var az:Number = sa.zIndex;
			var bz:Number = sb.zIndex;
			
			if (az > bz) {
				return 1;
			}else {
				if (az < bz) {
					return -1;
				}else {
					
					
					if (a.priority == b.priority) {
						
						if (ap.y > bp.y || ap.x > bp.x) {
							return 1;
						}
						
						return 0;
					}else {
						if (a.priority > b.priority) {
							
							return 1;
						}else {
							return -1;
						}
					}
				}
			}
		}
	}

}